<?php
class Default_Model_DbTable_ObjectLibPhoto extends Zend_Db_Table_Abstract {
	protected $_name = 'object_lib_photo';
	
	public function addOLP($oId, $name , $img, $sDesc, $lDesc, $mSlide, $position, $stt){
		$data = array(
					"OBJECT_ID" => $oId,
					"NAME" => $name,
					"IMG" => $img,
					"SHORT_DESCRIPTION" => $sDesc,
					"LONG_DESCRIPTION" => $lDesc,
					"MODE_SLIDE" => $mSlide,
					"POSITION" => $position,
					"STATUS" => $stt
				);
		return $this->insert($data);
	}
	
	public function updateObjectLibPhoto($id, $object_id, $name, $img, $short_description, $long_description, $mode_slide, $position, $status) {
		$data = array (
				'OBJECT_ID' => $object_id,
				'NAME' => $name,
				'IMG' => $img,
				'SHORT_DESCRIPTION' => $short_description,
				'LONG_DESCRIPTION' => $long_description,
				'MODE_SLIDE' => $mode_slide,
				'POSITION' => $position,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function removeObjectLibPhoto($id) {
		return $this->delete ( "ID = $id" );
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function activeObjectLibPhoto($id) {
		$lv = $this->fetchRow ( "ID = $id" );
		if ($lv ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function getAllObjectLibPhoto() {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_lib_photo AS olp", array (
				"olp.ID",
				"olp.NAME",
				"olp.IMG",
				"olp.SHORT_DESCRIPTION",
				"olp.LONG_DESCRIPTION",
				"olp.MODE_SLIDE",
				"olp.POSITION",
				"olp.STATUS" 
		) )->joinLeft ( "object AS o", "o.ID = olp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->group("olp.ID");
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectLibPhotoByObjectId($object_id, $position) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		try {
			$rows = $this->fetchAll ( "OBJECT_ID = $object_id AND STATUS = 1 AND POSITION = $position", "ID DESC" );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectLibPhotoByObjectName($object_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_lib_photo AS olp", array (
				"olp.ID",
				"olp.NAME",
				"olp.IMG",
				"olp.SHORT_DESCRIPTION",
				"olp.LONG_DESCRIPTION",
				"olp.MODE_SLIDE",
				"olp.STATUS" 
		) )->where ( "olp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = olp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->where ( "o.OBJECT_NAME= ?", $object_name );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectLibPhotoById($id) {
		/* $select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_lib_photo AS olp", array (
				"olp.ID",
				"olp.NAME",
				"olp.IMG",
				"olp.SHORT_DESCRIPTION",
				"olp.LONG_DESCRIPTION",
				"olp.MODE_SLIDE",
				"olp.POSITION",
				"olp.STATUS" 
		) )->where ( "olp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = olp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->where ( "olp.ID= ?", $id );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray (); */
		return $this->fetchRow($id);
	}
	
	public function GetByID($id){
		$sql = $this->select()->from($this->_name,array("*"))->where("ID = $id");
		return $this->fetchRow($sql);
	}
	
	public function getObjectLibPhotoByName($name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_lib_photo AS olp", array (
				"olp.ID",
				"olp.NAME",
				"olp.IMG",
				"olp.SHORT_DESCRIPTION",
				"olp.LONG_DESCRIPTION",
				"olp.MODE_SLIDE",
				"olp.STATUS" 
		) )->where ( "olp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = olp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->where ( "olp.NAME= ?", $name );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
}

